package itbour.onetouchshow.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.andview.adapter.BaseRecyclerHolder;
import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import itbour.onetouchshow.App;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.BaseRefreshAdapter;
import itbour.onetouchshow.bean.LoadVideoUserDocBg;
import itbour.onetouchshow.bean.page.Page;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.ImageLoader;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by Yang on 2017/12/5.
 */

@Deprecated
public class BgSelectorWindow extends PopupWindow {

    public static final int ON_REFRESH = 1;
    private static final int ON_LOAD = 2;
    public ItemSelectListener itemSelectListener;
    private List<LoadVideoUserDocBg.ListBean> mdata = new ArrayList<>();
    private View mPopView;
    private Page page;
    private int pullStatus;
    private int mPageIndex = 0;
    private XRefreshView xRefreshView;
    private VideoBgViewpagerAdapter adapter;
    private String mDocId;
    private Context context;

    public BgSelectorWindow(Context context, String docId, BgSelectorWindow.ItemSelectListener itemSelectListener) {
        this.itemSelectListener = itemSelectListener;
        this.mDocId = docId;
        //  this.mdata = data;
        this.context = context;
        initUI(context);
    }

    private void initUI(Context activity) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mPopView = inflater.inflate(R.layout.view_popup_bg_select, null);

        setContentView(mPopView);
        setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
        setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        setFocusable(true);
        // 点击外面的控件也可以使得PopUpWindow dimiss
        setOutsideTouchable(true);
        //ColorDrawable dw = new ColorDrawable(0xffffffff);//0xb0000000
        ColorDrawable dw = new ColorDrawable(0x00000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        // setBackgroundDrawable(dw);//半透明颜色
        setAnimationStyle(R.style.mypopwindow_anim_style);
        iniView(activity);

    }

    private void iniView(Context activity) {
        RecyclerView recyclerView = mPopView.findViewById(R.id.recyclerView);
        xRefreshView = mPopView.findViewById(R.id.id_xfresh);
        xRefreshView.setAutoRefresh(false);
        xRefreshView.setAutoLoadMore(true);
        xRefreshView.setPinnedTime(1000);
        xRefreshView.stopLoadMore(false);
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setMoveForHorizontal(true);
        recyclerView.setHasFixedSize(true);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                pullStatus = ON_REFRESH;
                page.loadPage(true);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                pullStatus = ON_LOAD;
                page.loadPage(false);
            }
        });

        onLoadData();

        ImageView closeImage = mPopView.findViewById(R.id.id_ig_close);
        closeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        adapter = new VideoBgViewpagerAdapter(activity, mdata, R.layout.item_bg_seletor);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        recyclerView.setAdapter(adapter);
        adapter.setCustomLoadMoreView(new XRefreshViewFooter(activity));


    }

    public void onLoadData() {
        page = new Page() {
            @Override
            public void load(int param1, int param2) {
                mPageIndex = param1;
                loadVideoUserDocBg();
            }
        };
        pullStatus = ON_REFRESH;
        page.loadPage(true);
    }

    public void loadVideoUserDocBg() {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", mDocId);
        //pageNum	是
        stringObjectHashMap.put("pageNum", mPageIndex);
        stringObjectHashMap.put("pageSize", AppConst.PAGE_SIZE);
        HttpUtils.getInstance().executeGet(AppConfig.GETVIDEOUSERDOCBGLIST_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                page.finishLoad(false);
                //T_.showToastReal(exception);
                L_.e("bg onSuccess" + exception);
                //endNetRequse(null);
            }

            @Override
            protected void onSuccess(String response) {
                L_.e("bg onSuccess" + response);
                page.finishLoad(true);
                LoadVideoUserDocBg loadVideoUserDocBg = new Gson().fromJson(response, LoadVideoUserDocBg.class);
                List<LoadVideoUserDocBg.ListBean> list = loadVideoUserDocBg.getList();
                if (list != null && list.size() > 0) {
                    endNetRequse(list);
                } else {
                    xRefreshView.setLoadComplete(true);
                }

            }
        });
    }

    public void endNetRequse(List<LoadVideoUserDocBg.ListBean> data) {
        if (pullStatus == ON_LOAD) {
            mdata.addAll(data);
            xRefreshView.stopLoadMore();
        } else if (pullStatus == ON_REFRESH) {
            mdata = data;
            xRefreshView.stopRefresh();
            xRefreshView.setLoadComplete(false);
        }
        adapter.notifyDataSetChanged(mdata);
        pullStatus = 0;
    }

    public void setItemSelectListener(ItemSelectListener itemSelectListener) {
        this.itemSelectListener = itemSelectListener;
    }

    /**
     * Dialog按钮回调接口
     */
    public interface ItemSelectListener {
        void onSelect(LoadVideoUserDocBg.ListBean listBean);
    }

    public class VideoBgViewpagerAdapter extends BaseRefreshAdapter<LoadVideoUserDocBg.ListBean> {

        public VideoBgViewpagerAdapter(Context context, List<LoadVideoUserDocBg.ListBean> datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
        }

        @Override
        protected void convert(BaseRecyclerHolder var1, LoadVideoUserDocBg.ListBean listBean, int var3) {
            ImageView imageView = var1.getView(R.id.id_ig);
            TextView tvName = var1.getView(R.id.id_tv_name);
            TextView tvCount = var1.getView(R.id.id_tv_count);
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            int width = UIUtils.WHD()[0] / 3 - UIUtils.dp2px(15);
            int height = width * listBean.getH() / listBean.getW();
            String imUrl = ImageLoader.getShrinkImageUrl(listBean.getThumb(), width, height);
            layoutParams.width = width;
            layoutParams.height = height;
            // ImageLoader.getInstance().showImage(context,imUrl, imageView);
            Glide.with(context).applyDefaultRequestOptions(new RequestOptions()
                    .apply(new RequestOptions()).placeholder(R.drawable.place_icon).
                            error(R.drawable.place_icon)).load(imUrl).into(imageView);

            tvName.setText(listBean.getName() + "");
            tvCount.setText(listBean.getUseCount() + "人使用");
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemSelectListener != null) {
                        dismiss();
                        itemSelectListener.onSelect(listBean);
                    }
                }
            });
        }
    }
}
